import util from '../../common/js/util'
import NProgress from 'nprogress'
import axios from 'axios'
import address from "../../../models/dict/address.js"  //任务字典模块
import moment from 'moment'

import task from "../../../models/dict/task.js"  //任务字典模块
import array from "../../../models/comm/array.js"  //数组操作模块
import yz from "../../../models/comm/yz.js"  //数组操作模块
import Vue from 'vue'
import py from '../../../models/comm/webPy';

import t100 from './info/t100.vue'
import t200 from './info/t200.vue'
import t300 from './info/t300.vue'
import t400 from './info/t400.vue'
import t500 from './info/t500.vue'
import t600 from './info/t600.vue'
import t700 from './info/t700.vue'
import t800 from './info/t800.vue'
import t900 from './info/t900.vue'
import tRoot from './info/tRoot.vue'
import tDefault from './info/tDefault.vue'


export default {



    data() {
        return {



            ShowSave: false,
            page: 1,
            filters: {},

            listLoading: false,
            total: 1,


            currCustomerOrder: {

            },
            node: {

            },
            nodeList: {

            },
            taskClassArray: []




        }
    },
    methods: {
        taskClassChange() {
            console.log('改变了任务类别');
            this.currObj = task.fmtTask(this.currObj);

        },

        fmtDate: function (row, col) {

            return moment(row.createTime).format("YYYY-MM-DD HH:mm:ss");

        },
        handleCurrentChange: function (params) {

        },
        saveTask: function (fun) {
            debugger
            this.currObj.companyCode = localStorage.companyCode;
            axios.post("/a", {
                class: "task",
                para: "saveTask",
                j: this.currObj

            }).then((res) => {
                if (res.data.re == "ok") {

                    if (!fun) {
                        this.$emit('afterSaveTask', res);
                    }
                    else {
                        fun(res)
                    }


                } else {
                    alert(JSON.stringify(res.data));
                }
            });
        },
        querySearchMember: function (query, cb) {
            var data = {
                "$or": [
                    { name: { $regex: query } },
                    { namePy: { $regex: py.firstLetter(query) } }
                ]
            }
            //  console.log(JSON.stringify(data));
            if (query !== '') {
                this.loading = true;
                axios.post("/a", {
                    class: "member",
                    para: "getMemberList",
                    j: {
                        data: data

                    }

                }).then((res) => {

                    this.loading = false;

                    var list = res.data.list;

                    list.forEach(function (member) {
                        member.value = member.name;

                    }, this);
                    cb(list)

                });

            }
        },
        selManager: function (params) {

            debugger
            var b = false;
            this.currObj.managers.forEach(function (manager) {
                if (params._id == manager._id) {

                    b = true;
                    return false;
                }

            }, this);
            if (!b) {
                this.currObj.managers.push({

                    name: params.name,
                    _id: params._id
                });

            }
            this.saveTask((res) => {

                this.$message({
                    message: '已经保存负责人!',
                    type: 'warning'  //success  warning  info  error
                });
                this.changeMemberMsg({
                    changeType: "add",
                    memberType: "managers",
                    memberId: params._id,
                    taskName: this.currObj.name
                })
            })
            //     console.log(JSON.stringify(this.currObj.managers));

        },
        selMember: function (params) {


            var b = false;
            this.currObj.members.forEach(function (member) {
                if (params._id == member._id) {

                    b = true;
                    return false;
                }

            }, this);
            if (!b) {
                this.currObj.members.push({

                    name: params.name,
                    _id: params._id
                });

            }
            this.saveTask((res) => {

                this.$message({
                    message: '已经保存参与人!',
                    type: 'warning'  //success  warning  info  error
                });

                this.changeMemberMsg({
                    changeType: "add",
                    memberType: "members",
                    memberId: params._id,
                    taskName: this.currObj.name
                })
            })
            //     console.log(JSON.stringify(this.currObj.managers));

        }

        ,
        handleClose: function (tag, tags, type) {

            tags.splice(tags.indexOf(tag), 1);
            var taskRole = ""
            switch (type) {
                case "members":
                    taskRole = "参与人"
                    break;
                case "managers":
                    taskRole = "负责人"
                    break;

            }

            var _this = this
            this.saveTask((res) => {

                this.$message({
                    message: '已经删除' + taskRole + '!',
                    type: 'warning'  //success  warning  info  error
                });

                this.changeMemberMsg({
                    changeType: "del",
                    memberType: type,
                    memberId: tag._id,
                    taskName: this.currObj.name
                })
            })
        },
        changeMemberMsg(o) {

            axios.post("/a", {
                class: "task",
                para: "changeMemberMsg",
                j: o
            }).then((res) => {

            });
        },
        getTaskClassName() {
            return task.getClassName(this.currObj.taskClassId)
        },
        loadTask() {
            console.log('重新构建了任务');

        }


    },
    props: {
        currObj: {

        },
        showTaskInfo: {}



    },

    mounted() {
        this.loadTask();
    },



    components: {

        t100,
        t200,
        t300,
        t400,
        t500,
        t600,
        t700,
        t800,
        t900,
        tRoot,
        tDefault


    }

}